/**
* @augments ksg.ui.grid.BaseGrid
* @class
* Works with a bizproc to show the detailed viewed of a given bizobj using the Kusog Grid jQuery UI widget.
* @meta-type jQuery widget
* @Depends ksg.gridCore.js
*/
ksg.ui.widgets.BizObjListGrid = $.extend({},ksg.ui.widgets.BaseWidget, ksg.ui.grid.BaseGrid, /** @lends ksg.ui.widgets.BizObjListGrid# */{
	_internalCreate: function() {
		var options = this.options;
		this.templates = ksg.ui.grid.templateHandlers.tmpl;
		this._setupGridDataHandler([]);
		this._IWidget_construct();
		ksg.App.bizProcMgr.registerWidget(this);
	},
	_internalDestroy: function() {
		this._IWidget_destruct();
	},
	_internalInitGrid: function() {
	},
	_setupGridDataHandler: function(bizObjList) {
		var idField = "id";
		var bizProc = ksg.App.bizProcMgr.getBizProc(this.getParentBizProcId());
		if(bizProc) 
			idField = bizProc.getKeyName();
		this.dataHandler = ksg.ui.grid.dataHandlerFactory.bizObjList(bizObjList,idField);
	},
	_internalSelectBizObj: function(bizObjId, fromRefresh) {
		if(!(bizObjId) && fromRefresh == true)
			return;
		var bizProc = ksg.App.bizProcMgr.getBizProc(this.getParentBizProcId());
		if(bizProc && bizObjId != bizProc.currentBizObjId) 
			bizProc.selectBizObj(bizObjId);
	},

	
	//--- IWidget	
	getWidgetType: function() {return ksg.ui.BizProcWidgetType.List;},
	
	//--- The following are required IBizObjListDisplay interface methods
	DrivenByBizProc: function(){return true;},
	
	setSelectedBizObjKeyValue: function(bizObjId) {
		this._selectBizObj(bizObjId, true);
	},
	
	showBizObjs: function(bizProc, bizObjCursor) {
		this._setupGridDataHandler(bizObjCursor.bizObjs);
		this._resetGridData();
	},
	
	MaxElements: function() {
		var max = this.options.maxElements;
		return (!(max))?-1:max;
	}
});

$.widget("ksg.bizObjListGrid", ksg.ui.widgets.BizObjListGrid);
ksg.ui.widgetMap["ksg-bizObjListGrid"] = function ($el, options) {
	$el.bizObjListGrid(options); 
};


ksg.ui.grid.dataHandlerFactory.bizObjList = function(bizObjList, idFieldName) {
	return $.extend({},{
		getList: function(id, options, onSuccess) {
			onSuccess(bizObjList);
		},
		getTotalPossibleCount: function(options, onSuccess) {
			onSuccess(bizObjList.length);
		},
		getIdFieldName: function() {
			return idFieldName;
		},
		getGeneralList: function(name,options) {
		},
		shouldRequestChildListData: function() {
			return false;
		},
		hasValue: function(obj, fieldName) {
			return ksg.bizobj.BizObj.hasValue(obj,fieldName);
		},
		getValue: function(obj, fieldName, defValue) {
			return ksg.bizobj.BizObj.getValue(obj,fieldName, defValue)
		},
		setValue: function(obj, fieldName, val) {
			//TODO setup set value
		}
	});
};
